package com.raining.inningz.ui.play;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.raining.inningz.R;
import com.raining.inningz.baseclass.BaseFragment;
import com.raining.inningz.constants.IDBConstants;
import com.raining.inningz.constants.IFragmentNameConstants;
import com.raining.inningz.constants.IParamConstants;
import com.raining.inningz.custom.validator.Form;
import com.raining.inningz.custom.validator.Validate;
import com.raining.inningz.custom.validator.validator.NotEmptyValidator;
import com.raining.inningz.database.dao.Matches;
import com.raining.inningz.database.dao.MatchesDao.Properties;
import com.raining.inningz.utils.Utils;

public class PlayMatchDetailsFragment extends BaseFragment implements
		OnClickListener {

	private LayoutInflater mInflater;
	private View view;

	private RelativeLayout relativeLayoutMatchInfo;
	private RelativeLayout relativeLayoutScoringRules;
	private RelativeLayout relativeLayoutToss;
	private RelativeLayout relativeLayoutVisitingTeam;
	private RelativeLayout relativeLayoutHostingTeam;
	private RelativeLayout relativeLayoutOfficials;
	
	private TextView txtMatchInfo;
	private TextView txtScoringRules;
	private TextView txtToss;
	private TextView txtVisitingTeam;
	private TextView txtVisitingTeamPlayer;
	private TextView txtHostingTeam;
	private TextView txtHostingTeamPlayer;
	private TextView txtOfficials;
	
	
	
	private PlayMatchInfoFragment playMatchInfoFragment;
	private PlayTossFragment playTossFragment;
	private PlayMatchOfficialsFragment playMatchOfficialsFragment;
	private PlayTeamRosterFragment playTeamRosterFragment;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(getArguments()!=null){
			
			
			matchID=getArguments().getString(IParamConstants.MATCH_ID);
			
			isEdit=getArguments().getBoolean(IParamConstants.IS_EDIT);
			}
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mInflater = inflater;

		view = inflater.inflate(R.layout.fragment_play_match_details,
				container, false);
		context = mInflater.getContext();

		initialiseUI();

		setListner();
		
		setData();

		return view;
	}
	

	private void setData() {
		
		List<Matches> matchesList=matchesDao.queryBuilder().where(Properties.MatchId.eq(matchID)).list();
		
		if(matchesList.size()>0){
			Matches matches=matchesList.get(0);
			
			txtMatchInfo.setText(matches.getMatchId());
			
			
			txtVisitingTeam.setText(matches.getVisitorsShortNm());
			txtHostingTeam.setText(matches.getHomeTeamShortNm());
		}
	}

	private void initialiseUI() {

		setHeaderIntialize(view);
		setLeftReturn();
		setLeftTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "InningZ");
		setRightTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "Save");
		setHeaderTitle(resources.getString(R.string.about), Color.BLACK);

		relativeLayoutMatchInfo = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutMatchInfo);
		relativeLayoutScoringRules = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutScoringRules);
		relativeLayoutToss = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutToss);
		relativeLayoutVisitingTeam = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutVisitingTeam);
		relativeLayoutHostingTeam = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutHostingTeam);
		relativeLayoutOfficials = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutOfficials);
		txtMatchInfo=(TextView)view.findViewById(R.id.txtMatchInfo);
		txtScoringRules=(TextView)view.findViewById(R.id.txtScoringRules);
		txtToss=(TextView)view.findViewById(R.id.txtToss);
		txtVisitingTeam=(TextView)view.findViewById(R.id.txtVisitingTeam);
		txtVisitingTeamPlayer=(TextView)view.findViewById(R.id.txtVisitingTeamPlayer);
		txtHostingTeam=(TextView)view.findViewById(R.id.txtHostingTeam);
		txtHostingTeamPlayer=(TextView)view.findViewById(R.id.txtHostingTeamPlayer);
		txtOfficials=(TextView)view.findViewById(R.id.txtOfficials);

	}

	private void setListner() {

		getBtnLeft().setOnClickListener(this);
		getBtnRight().setOnClickListener(this);
		relativeLayoutMatchInfo.setOnClickListener(this);
		relativeLayoutScoringRules.setOnClickListener(this);
		relativeLayoutToss.setOnClickListener(this);
		relativeLayoutVisitingTeam.setOnClickListener(this);
		relativeLayoutHostingTeam.setOnClickListener(this);
		relativeLayoutOfficials.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {

		if (v == getBtnLeft()) {
			fragmentManager.popBackStack();
		} else if (v == getBtnRight()) {
			
			Utils.exportDatabse(IDBConstants.DB_NAME, context);

		} else if (v == relativeLayoutMatchInfo) {
			
			Bundle args = new Bundle();
			args.putString(IParamConstants.MATCH_ID, matchID);
			args.putBoolean(IParamConstants.IS_EDIT, true);
			playMatchInfoFragment=new PlayMatchInfoFragment();
			
			playMatchInfoFragment.setArguments(args);
			changeFragment(playMatchInfoFragment, IFragmentNameConstants.PLAY_MATCH_INFO);

		} else if (v == relativeLayoutScoringRules) {

		} else if (v == relativeLayoutToss) {
			
			Bundle args = new Bundle();
			args.putString(IParamConstants.MATCH_ID, matchID);
			args.putBoolean(IParamConstants.IS_EDIT, true);
			playTossFragment=new PlayTossFragment();
			
			playTossFragment.setArguments(args);
			changeFragment(playTossFragment, IFragmentNameConstants.PLAY_TOSS);
			

		} else if (v == relativeLayoutVisitingTeam) {
			
			
			
			Bundle args = new Bundle();
			args.putString(IParamConstants.MATCH_ID, matchID);
			args.putBoolean(IParamConstants.IS_EDIT, true);
			
			
			
			playTeamRosterFragment=new  PlayTeamRosterFragment();
			
			playTeamRosterFragment.setArguments(args);
			
			changeFragment(playTeamRosterFragment, IFragmentNameConstants.PLAY_TEAM_ROSTER);
			

		} else if (v == relativeLayoutHostingTeam) {
			
			Bundle args = new Bundle();
			args.putString(IParamConstants.MATCH_ID, matchID);
			args.putBoolean(IParamConstants.IS_EDIT, true);
			
			
			
			playTeamRosterFragment=new  PlayTeamRosterFragment();
			
			playTeamRosterFragment.setArguments(args);
			
			changeFragment(playTeamRosterFragment, IFragmentNameConstants.PLAY_TEAM_ROSTER);

		} else if (v == relativeLayoutOfficials) {
			
			
			
			Bundle args = new Bundle();
			args.putString(IParamConstants.MATCH_ID, matchID);
			args.putBoolean(IParamConstants.IS_EDIT, true);
			playMatchOfficialsFragment=new PlayMatchOfficialsFragment();
			
			playMatchOfficialsFragment.setArguments(args);
			changeFragment(playMatchOfficialsFragment, IFragmentNameConstants.PLAY_MATCH_OFFICIALS);
			
		}

	}
}
